// mircoApp/new-auction/Pending_goods/Pending_goods.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hostUrl: "",
    list: [],
    isLoadMore: true,
    totalCount: 0,
    page: 1,
    total_page: 0,
    selectItem: null,
    show: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let language = wx.getStorageSync("langIndex");
    this.setData({
      hostUrl: app.data.host + "/plugins/new-auction/assets/image/",
      language: language.en
    })
    this.init();
    this.getData();
    this.getPoint();
  },
  init() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      list: []
    })
  },
  closePopup() {
    this.setData({
      show: false
    })
  },
  pickUpChild_popup(e) {
    let json = {
      new_auction_activity_id: e.detail.id,
      new_auction_pick_num: e.detail.value
    }; 
    wx.navigateTo({
      url: `/packageD/buy/myOrder_v2/myOrder_v2?tag=new_auction_mentioned&new_auction_activity_id=${json.new_auction_activity_id}&new_auction_pick_num=${json.new_auction_pick_num}`,
    })
    this.setData({
      show: false,
      selectItem: null
    })
  },
  async mentioned_tap(e) {
    let {
      item
    } = e.currentTarget.dataset;
    if(item.stock == 0){
      app.tips('库存不足')
      return
    }
    this.setData({
      selectItem: item,
      show: true
    })
    console.log(item)
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-auction.frontend.activity.activityGoodsList")
    })
    if (data.result) {
      this.data.isLoadMore = true;
      this.setData({
        list: data.data.data,
        total_page: data.data.last_page,
      })
    } else {
      app.tips(data.msg)
    }
  },
  async getPoint(){
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-auction.frontend.activity.getPoint")
    })
    if (data.result) {
      this.setData({
        totalCount: data.point
      })
    } else {
      app.tips(data.msg)
    }
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false;
    if (this.page >= this.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.new-auction.frontend.activity.activityGoodsList"),
        data: {
          page: this.data.page
        }
      })
      this.data.isLoadMore = true;
      if (data.result) {
        this.setData({
          list: this.data.list.concat(data.data.data),
        })
      } else {
        app.tips(data.msg);
        this.data.isLoadMore = false;
        this.setData({
          page: this.data.page - 1
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})